package com.cloud.manage.utils;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.Data;
import org.springframework.stereotype.Component;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.GZIPInputStream;

/**
 * @Author zfj
 * @Date 2021-03-25 09:55:00
 * 定时任务工具类
 */
@Component
@Data
public class ClzJobUnit {

    public String jobName = "定时任务未设置job名称";
    public Integer successNum = 0;
    public Integer errorNum = 0;
    //更新的数据时间
    public String selectTime = "";

    public static void main(String[] args) {
        System.out.println((int)Math.ceil((double)4/345));
    }



    //任务开始
    public void jobInit() {
        XxlJobHelper.log(jobName + "开始执行。");
    }

    //任务正常结束
    public void jobDestroy() {
        XxlJobHelper.handleSuccess(jobName + "执行完毕：<br/>"
                + "&emsp;&emsp;成功更新数据：" + successNum + "条<br/>"
                + "&emsp;&emsp;失败更新数据：" + errorNum + "条<br/>"
                + "&emsp;&emsp;数据更新对应时间：" + selectTime + "<br/>");
    }

    //任务异常结束
    //errorMsg（"&emsp;&emsp;" + 填写自己需要的内容 + "<br/>"），如需换行显示，请重复该格式
    public void jobError(String errorMsg) {
        XxlJobHelper.handleFail(jobName + "任务未能正常结束，原因如下：<br/>" + errorMsg);
    }

    //任务完成后初始化原始数据
    public void jobInitString() {
        jobName = "定时任务未设置job名称";
        successNum = 0;
        errorNum = 0;
        selectTime = "";
    }

    //记录报错信息
    public void jobErrorInfo(Exception e) {
        //异常e的详细错误信息
        XxlJobHelper.handleFail(jobName + "执行错误，日志信息如下：<br/>"
                + getExceptionStr(e));
    }

    /**
     * 获取查询数据接口的最新时间，判断数据是否符合查询时间的条件
     */
    public void latestforecasttime(JobParame jobParame, String latestforecasttimeUrl) {

        String timeStr = getLatestforecasttimeStr(latestforecasttimeUrl);

        //测试数据
        //String timeStr = "2021-03-25 20:00:00";
        DateTime timeDate = DateUtil.parse(timeStr, "yyyy-MM-dd HH:mm:ss");

        Date nowDate = new Date();
        //测试数据
        // Date nowDate = DateUtil.parse("2021-03-26 20:00:00", "yyyy-MM-dd HH:mm:ss");
        String nowStr = DateUtil.formatDate(nowDate);

        DateTime timeDateDay = DateUtil.parse(timeStr, "yyyy-MM-dd");
        DateTime nowStrDay = DateUtil.parse(nowStr, "yyyy-MM-dd");
        //如果最新数据时间天数在当前时间天数之前，说明没有最新数据，失败返回
        if (timeDateDay.before(nowStrDay)) {
            jobParame.setNormalEndFlag(false);
            jobParame.setErrorMsg("&emsp;&emsp;无最新数据<br/>"
                    + "&emsp;&emsp;接口数据最新时间:" + timeStr + "<br/>");
            return;
        }

        //如果最新时间在当前时间13小时前(例，晚九点查到的数据还是早八点的)，说明无最新数据，失败返回(时间放宽点)
        long betweenMs = DateUtil.betweenMs(timeDate, nowDate);
        if (betweenMs > 13 * 60 * 60 * 1000) {
            jobParame.setNormalEndFlag(false);
            jobParame.setErrorMsg("&emsp;&emsp;无最新数据<br/>"
                    + "&emsp;&emsp;接口数据最新时间:" + timeStr + "<br/>");
            return;
        }

    }

    public String getLatestforecasttimeStr(String latestforecasttimeUrl) {
        String latestforecasttime = "";
        for (int i = 0; i < 5; i++) {
            try {
                latestforecasttime = HttpUtil.get(latestforecasttimeUrl, 10 * 1000);
                if (StrUtil.isNotBlank(latestforecasttime)) {
                    break;
                }
            } catch (Exception e) {
            }
        }
        JSONObject jsonObject = JSONUtil.parseObj(latestforecasttime);
        JSONArray jsonArray = JSONUtil.parseArray(jsonObject.get("data"));
        String timeStr = jsonArray.get(0).toString();
        //部分最新时间没有后面，格式不对
        if (timeStr.length() == 16) {
            timeStr += ":00";
        }
        return timeStr;
    }

    /**
     * 查询当前最新时间，比对数据库，如果数据库没有，则进行更新
     */
    public String latestforecasttimeByNowTime(JobParame jobParame, String latestforecasttimeUrl, String lastUpdateTime) {

        String timeStr = getLatestforecasttimeStr(latestforecasttimeUrl);

        if (StrUtil.isBlankIfStr(lastUpdateTime)) {
            return timeStr.replace("-", "").replace(" ", "").replace(":", "").substring(0, 12);
        }
        Date beforeDate = DateUtil.parse(lastUpdateTime, "yyyy-MM-dd HH:mm:ss.S");
        Date afterDate = DateUtil.parse(timeStr, "yyyy-MM-dd HH:mm:ss");

        //如果不等，说明已经更新过了，或者数据时间不是最新的
        if (!afterDate.after(beforeDate)) {
            jobParame.setNormalEndFlag(false);
            jobParame.setErrorMsg("&emsp;&emsp;无最新数据<br/>"
                    + "&emsp;&emsp;最后数据更新时间（当前时间）:" + lastUpdateTime + "<br/>"
                    + "&emsp;&emsp;接口数据最新时间:" + timeStr + "<br/>");
            return "";
        }
        return timeStr.replace("-", "").replace(" ", "").replace(":", "").substring(0, 12);
    }

    /**
     * 查询当前最新时间
     */
    public String latestforecasttimeByNowTime(JobParame jobParame, String latestforecasttimeUrl) {
        String timeStr = getLatestforecasttimeStr(latestforecasttimeUrl);
        return timeStr.replace("-", "").replace(" ", "").replace(":", "").substring(0, 12);
    }

    //获取当前时间的查询格式
    public String getNowTimeStr(String format) {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        return simpleDateFormat.format(date);

    }


    /**
     * 根据当前时间获得应该使用的查询时间条件，不能把时间写死，有可能出现手动执行定时任务的情况
     *
     * @return
     */
    public static String getSelectTimeStr() {

        Date nowDate = new Date();
        String timeStr = DateUtil.format(nowDate, "yyyyMMddHHmmss");
        Integer nowStrH = Integer.parseInt(timeStr.substring(8, 10));
        if (nowStrH < 20) {
            return timeStr.substring(0, 8) + "0800";
        } else {
            return timeStr.substring(0, 8) + "2000";
        }

    }

    public static String getSelectTimeStr24() {

        Date nowDate = new Date();
        String timeStr = DateUtil.format(nowDate, "yyyyMMddHHmmss");
        Integer nowStrH = Integer.parseInt(timeStr.substring(8, 10));
        if (nowStrH >= 7 && nowStrH <= 9) {
            return timeStr.substring(0, 8) + "0800";
        } else if (nowStrH >= 19 && nowStrH <= 21) {
            return timeStr.substring(0, 8) + "2000";
        } else if (nowStrH >= 10 && nowStrH <= 12) {
            return timeStr.substring(0, 8) + "1100";
        } else if (nowStrH >= 13 && nowStrH <= 15) {
            return timeStr.substring(0, 8) + "1400";
        } else if (nowStrH >= 16 && nowStrH <= 18) {
            return timeStr.substring(0, 8) + "1700";
        } else if (nowStrH >= 22 && nowStrH <= 24) {
            return timeStr.substring(0, 8) + "2300";
        } else {
            return timeStr.substring(0, 8) + "0800";
        }

    }


    // 风速
    public static String windForceCalculation(String u, String v) {
        double dataDouble = Math.round(Math.hypot(Double.parseDouble(u), Double.parseDouble(v)) * 10) / 10.0;
        if (dataDouble < 100) {
            return NumberUtil.roundStr(dataDouble, 1);
        } else {
            return "3.5";
        }
    }


    // 风等级转换
    public static String windForceGrade(double dataDouble) {
        String result;
        if (dataDouble < 0.3) {
            //默认应该为0级，邹总不想看到0级，修改为1级
            result = "1";
        } else if (dataDouble >= 0.3 && dataDouble <= 1.5) {
            result = "1";
        } else if (dataDouble < 3.4 && dataDouble > 1.5) {
            result = "2";
        } else if (dataDouble >= 3.4 && dataDouble < 5.5) {
            result = "3";
        } else if (dataDouble >= 5.5 && dataDouble < 8.0) {
            result = "4";
        } else if (dataDouble >= 8.0 && dataDouble < 10.8) {
            result = "5";
        } else if (dataDouble >= 10.8 && dataDouble < 13.9) {
            result = "6";
        } else if (dataDouble >= 13.9 && dataDouble < 17.2) {
            result = "7";
        } else if (dataDouble >= 17.2 && dataDouble < 20.8) {
            result = "8";
        } else if (dataDouble >= 20.8 && dataDouble < 24.5) {
            result = "9";
        } else if (dataDouble >= 24.5 && dataDouble < 28.5) {
            result = "10";
        } else if (dataDouble >= 28.5 && dataDouble < 32.7) {
            result = "11";
        } else if (dataDouble >= 32.7 && dataDouble < 8888) {
            result = "12";
        } else {
            //异常数据返回一个正常数据，如2,3
            result = "2";
        }
        return result;
    }

    //查找时间对应的所需时序
    public static String[] getValidtimeStrAry(String selectTime, long hStr, long minStr, long timeNum) {
        //如果小于当前查找时间节点，默认需要去查找第二天的
        if (StrUtil.isNotBlank(selectTime)) {
            selectTime = selectTime.substring(8, 10);
        } else {
            selectTime = getSelectTimeStr().substring(8, 10);
        }

        //存储不重复时序
        Set<String> strSet = new TreeSet<>(new Comparator<String>() {
            public int compare(String str1, String str2) {
                // 这里compareTo()方法是对字符串中各个字符的Unicode值进行比较，如果你想实现按拼音排序未必可行
                // 可以调换一下str1和str2的位置试试看有什么不同效果
                return str1.compareTo(str2);
            }
        });


        //路程所需小时数，向上取整
        long hAddStr = (long) Math.ceil((double) (minStr + timeNum) / 60);
        //除以时序对应的3，循环次数
        //获取后续时序
        for (int i = 0; i < hAddStr; i++) {
            long hTemp = hStr;
            hTemp += i;
            strSet.add(getTime(hTemp, selectTime));
        }
        List<String> list = new ArrayList(strSet);
        List<String> returnList = new ArrayList<>();
        returnList.add("minvalidtime=" + list.get(0) + "&maxvalidtime=" + list.get(list.size() - 1));

        for (int i = 1; i < 10; i++) {
            int num1 = Integer.parseInt(list.get(0));
            num1 += i * 24;
            String startStr1 = num1 + "";
            if (num1 < 100) {
                startStr1 = "0" + num1;
            }


            int num2 = Integer.parseInt(list.get(list.size() - 1));
            num2 += i * 24;
            String startStr2 = num2 + "";
            if (num2 < 100) {
                startStr2 = "0" + num2;
            }

            if (num2 <= 240) {
                returnList.add("minvalidtime=" + startStr1 + "&maxvalidtime=" + startStr2);
            }
        }
        return returnList.toArray(new String[0]);
    }

    //根据小时数，计算对应时序，留意有第二天的情况
    public static String getTime(long hStr, String selectTimeStr) {


        if (hStr < 8 && "08".equals(selectTimeStr)) {
            hStr += 24;
        } else if (hStr < 20 && "20".equals(selectTimeStr)) {
            hStr += 24;
        }

        // 后续使用时序按0800计算，统一hStr格式
        if ("20".equals(selectTimeStr)) {
            hStr -= 12;
        }

        String validtime = "";
        if (hStr >= 8 && hStr < 11) {
            validtime = "003";
        } else if (hStr >= 11 && hStr < 14) {
            validtime = "006";
        } else if (hStr >= 14 && hStr < 17) {
            validtime = "009";
        } else if (hStr >= 17 && hStr < 20) {
            validtime = "012";
        } else if (hStr >= 20 && hStr < 23) {
            validtime = "015";
        } else if (hStr >= 23 && hStr < 26) {
            validtime = "018";
        } else if (hStr >= 26 && hStr < 29) {
            validtime = "021";
        } else if (hStr >= 29 && hStr < 32) {
            validtime = "024";
        } else if (hStr >= 32 && hStr < 35) {
            validtime = "027";
        } else if (hStr >= 35 && hStr < 38) {
            validtime = "030";
        } else if (hStr >= 38 && hStr < 41) {
            validtime = "033";
        } else if (hStr >= 41 && hStr < 44) {
            validtime = "036";
        } else if (hStr >= 44 && hStr < 47) {
            validtime = "039";
        } else if (hStr >= 47 && hStr < 50) {
            validtime = "042";
        } else if (hStr >= 50 && hStr < 53) {
            validtime = "045";
        } else if (hStr >= 53 && hStr < 56) {
            validtime = "048";
        }
        return validtime;
    }

    //计算对应影响值

    /**
     * @param eleNum    对应的ele
     * @param eleStrNum 对应具体的值
     * @return
     */
    public Map<String, String> flagCalculation(int eleNum, double eleStrNum) {
        Map<String, String> map = new HashMap<>();
        map.put("flag", "0");
        map.put("impactSuggestion", "");
        if (eleStrNum == 9999) {
            return map;
        }
        //气温影响
        if (eleNum == 0) {
            String eleStrNumFormat = NumberUtil.roundStr(eleStrNum, 0);
            if (eleStrNum < 33) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "气温对交通出行基本无影响。");*/
            } else if (eleStrNum >= 33 && eleStrNum < 36) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计气温达到" + eleStrNumFormat + "度，建议：适当减速行驶。");
            } else if (eleStrNum >= 36 && eleStrNum < 39) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计气温达到" + eleStrNumFormat + "度，建议：出行前检查车况。运输中适当减速行驶。");
            } else if (eleStrNum >= 39 && eleStrNum < 42) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计气温达到" + eleStrNumFormat + "度，建议：开车前检查车况，严防车辆自燃；保证睡眠充足，限速行驶。");
            } else if (eleStrNum >= 42) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计气温达到" + eleStrNumFormat + "度，建议：开车前检查车况、水箱和电路，严防车辆自燃；保证睡眠充足，限速行驶，必要时停驶。");
            }
        }
        //相对湿度影响(暂时没有对应判断值)
        /*else if (eleNum == 1) {
            if (eleStrNum >= 80 && eleStrNum <= 90) {map.put("flag", "2");
                map.put("impactSuggestion", "高温对交");
            } else if (eleStrNum > 90) {map.put("flag", "2");
                map.put("impactSuggestion", "高温对交");
            }
        }*/
        //相态影响
        else if (eleNum == 3) {
            String eleStrNumFormat = NumberUtil.roundStr(eleStrNum, 0);
            if (eleStrNum > 0 && eleStrNum < 15) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "行车视距大于800米，可能造成道路湿滑，注意保持车距，避让来往车辆。");*/
            } else if (eleStrNum >= 15 && eleStrNum < 30) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距500至800米。建议：车速55-60公里每小时，保持车距。");
            } else if (eleStrNum >= 30 && eleStrNum < 40) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距300至500米。建议：车速50-55公里每小时，保持车距。");
            } else if (eleStrNum >= 40 && eleStrNum < 60) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距200至300米。建议：车速40-50公里每小时，注意各类警示牌和提示标志。");
            } else if (eleStrNum >= 60 && eleStrNum < 80) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距100至200米。建议：车速30-40公里每小时，避让来往车辆。");
            } else if (eleStrNum >= 80 && eleStrNum < 120) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距50至100米。建议：车速20-30公里每小时，必要时停驶。");
            } else if (eleStrNum >= 120) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距不足5米。建议：前方道路车辆停驶。 ");
            }
        }
        //能见度影响
        else if (eleNum == 4) {
            String eleStrNumFormat = NumberUtil.roundStr(eleStrNum, 0);
            if (eleStrNum >= 10000) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "行车视距大于800米，可能造成道路湿滑，注意保持车距，避让来往车辆。");*/
            } else if (eleStrNum >= 1000 && eleStrNum < 10000) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "有轻雾，车辆可正常行驶.");*/
            } else if (eleStrNum >= 500 && eleStrNum < 1000) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "前方有雾，车辆应注意保持车距。");*/
            } else if (eleStrNum >= 200 && eleStrNum < 500) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计能见度" + eleStrNumFormat + "米。建议：减速慢行，加大车距。");
            } else if (eleStrNum >= 100 && eleStrNum < 200) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计能见度" + eleStrNumFormat + "米。建议：开启雾灯、近光灯、示廓灯和前后位灯，车速40-60公里每小时，车距100米以上。");
            } else if (eleStrNum >= 50 && eleStrNum < 100) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计能见度" + eleStrNumFormat + "米。建议：开启雾灯、近光灯、示廓灯、前后位灯和危险报警闪光灯，车速20-40公里每小时，车距50米以上。");
            } else if (eleStrNum < 50) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计能见度" + eleStrNumFormat + "米。建议：开启雾灯、近光灯、示廓灯、前后位灯和危险报警闪光灯，车速不超过20公里每小时，并从最近的出口驶离高速公路。");
            }
        }
        //风力影响
        else if (eleNum == 5) {
            if (eleStrNum < 8) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "车辆可正常行驶");*/
            } else if (eleStrNum >= 8 && eleStrNum < 13.9) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计风速达到" + windForceGrade(eleStrNum) + "级。建议：适当降低车速，保持平稳运行。");
            } else if (eleStrNum >= 13.9 && eleStrNum < 17.2) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计风速达到" + windForceGrade(eleStrNum) + "级。建议：降低车速，保持平稳运行。");
            } else if (eleStrNum >= 17.2 && eleStrNum < 20.8) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计风速达到" + windForceGrade(eleStrNum) + "级。建议：降低车速；途径弯道、山谷、峡口堤等地，提前缓慢行驶，以防翻车。  ");
            } else if (eleStrNum >= 20.8) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计风速达到" + windForceGrade(eleStrNum) + "级。建议：降低车速；途径弯道、山谷、峡口等地，提前缓慢行驶，以防翻车。必要时停驶。");
            }
        }

        //一小时降水影响
        else if (eleNum == 6) {
            String eleStrNumFormat = NumberUtil.roundStr(eleStrNum, 0);
            if (eleStrNum > 0 && eleStrNum < 10) {
                /*map.put("flag", "0");
                map.put("impactSuggestion", "行车视距大于800米，可能造成道路湿滑，注意保持车距，避让来往车辆。");*/
            } else if (eleStrNum >= 10 && eleStrNum < 15) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距500至800米。建议：车速55-60公里每小时，保持车距。");
            } else if (eleStrNum >= 15 && eleStrNum < 30) {
                map.put("flag", "1");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距300至500米。建议：车速50-55公里每小时，保持车距。");
            } else if (eleStrNum >= 30 && eleStrNum < 50) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距200至300米。建议：车速40-50公里每小时，注意各类警示牌和提示标志。");
            } else if (eleStrNum >= 50 && eleStrNum < 70) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距100至200米。建议：车速30-40公里每小时，避让来往车辆。");
            } else if (eleStrNum >= 70 && eleStrNum < 100) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距50至100米。建议：车速20-30公里每小时，必要时停驶。");
            } else if (eleStrNum >= 100) {
                map.put("flag", "2");
                map.put("impactSuggestion", "预计降雨量达到" + eleStrNumFormat + "毫米，行车视距不足5米。建议：前方道路车辆停驶。 ");
            }
        }
        return map;
    }


    public static String tq(String code) {
        int dataInt = Integer.parseInt(code);
        String tqc = "";
        switch (dataInt) {
            case 0:
                tqc = "晴";
                break;
            case 1:
                tqc = "多云";
                break;
            case 2:
                tqc = "阴";
                break;
            case 3:
                tqc = "阵雨";
                break;
            case 4:
                tqc = "雷阵雨";
                break;
            case 5:
                tqc = "雷阵雨并伴有冰雹";
                break;
            case 6:
                tqc = "雨夹雪";
                break;
            case 7:
                tqc = "小雨";
                break;
            case 8:
                tqc = "中雨";
                break;
            case 9:
                tqc = "大雨";
                break;
            case 10:
                tqc = "暴雨";
                break;
            case 11:
                tqc = "大暴雨";
                break;
            case 12:
                tqc = "特大暴雨";
                break;
            case 13:
                tqc = "阵雪";
                break;
            case 14:
                tqc = "小雪";
                break;
            case 15:
                tqc = "中雪";
                break;
            case 16:
                tqc = "大雪";
                break;
            case 17:
                tqc = "暴雾";
                break;
            case 18:
                tqc = "雾";
                break;
            case 19:
                tqc = "冻雨";
                break;
            case 20:
                tqc = "沙尘暴";
                break;
            case 21:
                tqc = "小雨-中雨";
                break;
            case 22:
                tqc = "中雨-大雨";
                break;
            case 23:
                tqc = "大雨-暴雨";
                break;
            case 24:
                tqc = "暴雨-大暴雨";
                break;
            case 25:
                tqc = "大暴雨-特大暴雨";
                break;
            case 26:
                tqc = "小雪-中雪";
                break;
            case 27:
                tqc = "中雪—大雪";
                break;
            case 28:
                tqc = "大雪-暴雪";
                break;
            case 29:
                tqc = "浮尘";
                break;
            case 30:
                tqc = "扬沙";
                break;
            case 31:
                tqc = "强沙尘暴";
                break;
            case 32:
                tqc = "阵雨";
                break;
            case 33:
                tqc = "雪";
                break;
            case 34:
                tqc = "雨";
                break;
            case 35:
                tqc = "小雪";
                break;
            case 53:
                tqc = "霾";
                break;
            default:
                tqc = "";
                break;
        }
        return tqc;
    }

    public static String dealweather(String w1, String w2, String w3, String w4) {
        //晴，晴，晴，晴
        if (w1.equals(w2) && w2.equals(w3) && w3.equals(w4)) {
            return w1;
        }
        //晴，晴，晴，霾
        if (w1.equals(w2) && w2.equals(w3)) {
            return w1 + "转" + w4;
        }
        //晴，晴，霾，晴
        if (w1.equals(w2) && w2.equals(w4)) {
            return w1 + "转" + w3;
        }
        //晴，晴，霾，霾
        if (w1.equals(w2) && w3.equals(w4)) {
            return w1 + "转" + w3;
        }
        //晴，霾，霾，霾
        if (w2.equals(w3) && w3.equals(w4)) {
            if (StrUtil.isBlankIfStr(w1)) {
                return w4;
            }
            return w1 + "转" + w4;
        }
        //晴，霾，霾，晴
        if (w2.equals(w3) && w1.equals(w4)) {
            if (StrUtil.isBlankIfStr(w1)) {
                return w3;
            }
            return w1 + "转" + w3;
        }
        //晴，霾，晴，霾
        if (w2.equals(w4) && w1.equals(w3)) {
            return w1 + "转" + w2;
        }
        //晴，霾，羽，霾
        if (w2.equals(w4)) {
            return w2;
        }
        return w2 + "转" + w4;
    }

    //计算最大值经纬度
    public String getMaxLonLat(String str) {
        String maxTemp = (Double.parseDouble(str) + 0.04) + "0000";
        maxTemp = maxTemp.substring(0, maxTemp.indexOf(".") + 3);
        return maxTemp;
    }

    /**
     * 根据接口精度格式化经纬度，保留两位小数
     *
     * @param str 原始经纬度字符串
     * @return
     */
    public static String getWeatherLonLat(String str) {
        //保留两位小数字符串
        String str1 = NumberUtil.roundStr(str, 2);
        //获取最后一位数值
        String substring = str1.substring(str1.length() - 1, str1.length());
        int num1 = Integer.parseInt(substring);
        String returnStr = "";
        if (num1 < 5) {
            returnStr = str1.substring(0, str1.length() - 1) + "0";
        } else {
            returnStr = str1.substring(0, str1.length() - 1) + "5";
        }
        return returnStr;
    }

    public static String getExceptionStr(Exception e) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw, true);
        e.printStackTrace(pw);
        String stackTraceString = sw.getBuffer().toString();
        return stackTraceString;
    }

    //根据日期取得星期几
    public static String getWeek(Date date) {
        String[] weeks = {"日", "一", "二", "三", "四", "五", "六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int week_index = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (week_index < 0) {
            week_index = 0;
        }
        return weeks[week_index];
    }


    //风向判断
    public static String windDirection(double v, double u) {
        double direcdion = 999.9; //风向
        if (u > 0 & v > 0) {
            direcdion = 270 - Math.atan(v / u) * 180 / Math.PI;
        } else if (u < 0 & v > 0) {
            direcdion = 90 - Math.atan(v / u) * 180 / Math.PI;
        } else if (u < 0 & v < 0) {
            direcdion = 90 - Math.atan(v / u) * 180 / Math.PI;
        } else if (u > 0 & v < 0) {
            direcdion = 270 - Math.atan(v / u) * 180 / Math.PI;
        } else if (u == 0 & v > 0) {
            direcdion = 180;
        } else if (u == 0 & v < 0) {
            direcdion = 0;
        } else if (u > 0 & v == 0) {
            direcdion = 270;
        } else if (u < 0 & v == 0) {
            direcdion = 90;
        } else if (u == 0 & v == 0) {
            direcdion = 999.9;
        }
        String fx = "";
        if (direcdion == 0 || direcdion == 360) {
            fx = "北风";
        } else if (direcdion > 0 && direcdion < 90) {
            fx = "东北风";
        } else if (direcdion == 90) {
            fx = "东风";
        } else if (direcdion > 90 && direcdion < 180) {
            fx = "东南风";
        } else if (direcdion == 180) {
            fx = "南风";
        } else if (direcdion > 180 && direcdion < 270) {
            fx = "西南风";
        } else if (direcdion == 270) {
            fx = "西风";
        } else if (direcdion > 270 && direcdion < 360) {
            fx = "西北风";
        }
        return fx;
    }

    public static List<String[]> getUrlAryList(String url) throws Exception {
        String responseStr = "";
        for (int i = 0; i < 5; i++) {
            try {
                responseStr = HttpUtil.get(url, 10 * 1000);
                if (StrUtil.isNotBlank(responseStr)) {
                    break;
                }
            } catch (Exception e) {
            }
        }

        //获取到最新数据
        JSONObject jsonObjectTemp = JSONUtil.parseObj(responseStr);
        JSONArray jsonArrayTemp = JSONUtil.parseArray(jsonObjectTemp.get("data"));
        JSONObject jsonObject = JSONUtil.parseObj(jsonArrayTemp.get(0));
        JSONArray jsonArray = JSONUtil.parseArray(jsonObject.get("DS"));
        List<String[]> stringAryList = jsonArray.toList(String[].class);
        return stringAryList;
    }

    public static String getUrlUniversalResponseStr(String url) throws Exception {
        String responseStr = "";
        for (int i = 0; i < 5; i++) {
            try {
                responseStr = HttpUtil.get(url, 10 * 1000);
                if (StrUtil.isNotBlank(responseStr)) {
                    break;
                }
            } catch (Exception e) {
            }
        }
        return responseStr;
    }


    //获取天气的级别
    public static int getAQI(String so2Str, String NOStr, String PM10Str, String CoStr, String PM25Str, String O3Str) {
        // 取出当前时次的数据，入库时 的六个数据。
        double so2 = Double.parseDouble(so2Str);
        double NO = Double.parseDouble(NOStr);
        double PM10 = Double.parseDouble(PM10Str);
        double Co = Double.parseDouble(CoStr);
        double PM25 = Double.parseDouble(PM25Str);
        double O3 = Double.parseDouble(O3Str);
        // API的值
        double so2AQI = 0;
        double NOAQI = 0;
        double PM10AQI = 0;
        double CoAQI = 0;
        double PM25AQI = 0;
        double O3AQI = 0;
        // 以下数据的取值采用空气质量分指数计算方法
        // so2的计算
        if (so2 > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < so2 && so2 <= 150) {
                bph = 150;
                IAh = 50;
            } else if (so2 > 150 && so2 <= 500) {
                bph = 500;
                bpl = 150;
                IAl = 50;
                IAh = 100;
            } else if (so2 > 500 && so2 <= 650) {
                bph = 650;
                bpl = 500;
                IAl = 100;
                IAh = 150;
            } else if (so2 > 650 && so2 <= 800) {
                bph = 800;
                bpl = 650;
                IAl = 150;
                IAh = 200;
            }
            so2AQI = ((IAh - IAl) / (bph - bpl)) * (so2 - bpl) + IAl;
        }
        // co的计算
        if (Co > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < Co && Co <= 5) {
                bph = 5;
                IAh = 50;
            } else if (Co > 5 && Co <= 10) {
                bph = 10;
                bpl = 5;
                IAl = 50;
                IAh = 100;
            } else if (Co > 10 && Co <= 35) {
                bph = 35;
                bpl = 10;
                IAl = 100;
                IAh = 150;
            } else if (Co > 35 && Co <= 60) {
                bph = 60;
                bpl = 35;
                IAl = 150;
                IAh = 200;
            } else if (Co > 60 && Co <= 90) {
                bph = 90;
                bpl = 60;
                IAl = 200;
                IAh = 300;
            } else if (Co > 90 && Co <= 120) {
                bph = 120;
                bpl = 90;
                IAl = 300;
                IAh = 400;
            } else if (Co > 120 && Co <= 150) {
                bph = 150;
                bpl = 120;
                IAl = 400;
                IAh = 500;
            }
            CoAQI = ((IAh - IAl) / (bph - bpl)) * (Co - bpl) + IAl;
        }
        // O3的计算
        if (O3 > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < O3 && O3 <= 160) {
                bph = 160;
                IAh = 50;
            } else if (O3 > 160 && O3 <= 200) {
                bph = 200;
                bpl = 160;
                IAl = 50;
                IAh = 100;
            } else if (O3 > 200 && O3 <= 300) {
                bph = 300;
                bpl = 200;
                IAl = 100;
                IAh = 150;
            } else if (O3 > 300 && O3 <= 400) {
                bph = 400;
                bpl = 300;
                IAl = 150;
                IAh = 200;
            } else if (O3 > 400 && O3 <= 800) {
                bph = 800;
                bpl = 400;
                IAl = 200;
                IAh = 300;
            } else if (O3 > 800 && O3 <= 1000) {
                bph = 1000;
                bpl = 800;
                IAl = 300;
                IAh = 400;
            } else if (O3 > 1000 && O3 <= 1200) {
                bph = 1200;
                bpl = 1000;
                IAl = 400;
                IAh = 500;
            }
            O3AQI = ((IAh - IAl) / (bph - bpl)) * (O3 - bpl) + IAl;
        }

        // NO2de
        if (NO > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < NO && NO <= 100) {
                bph = 100;
                IAh = 50;
            } else if (NO > 100 && NO <= 200) {
                bph = 200;
                bpl = 100;
                IAl = 50;
                IAh = 100;
            } else if (NO > 200 && NO <= 700) {
                bph = 700;
                bpl = 200;
                IAl = 100;
                IAh = 150;
            } else if (NO > 700 && NO <= 1200) {
                bph = 1200;
                bpl = 700;
                IAl = 150;
                IAh = 200;
            } else if (NO > 1200 && NO <= 2340) {
                bph = 2340;
                bpl = 1200;
                IAl = 200;
                IAh = 300;
            } else if (NO > 2340 && NO <= 3090) {
                bph = 3090;
                bpl = 2340;
                IAl = 300;
                IAh = 400;
            } else if (NO > 3090 && NO <= 3840) {
                bph = 3840;
                bpl = 3090;
                IAl = 400;
                IAh = 500;
            }
            NOAQI = ((IAh - IAl) / (bph - bpl)) * (NO - bpl) + IAl;
        }

        if (PM25 > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < PM25 && PM25 <= 35) {
                bph = 35;
                IAh = 50;
            } else if (PM25 > 35 && PM25 <= 75) {
                bph = 75;
                bpl = 35;
                IAl = 50;
                IAh = 100;
            } else if (PM25 > 75 && PM25 <= 115) {
                bph = 115;
                bpl = 75;
                IAl = 100;
                IAh = 150;
            } else if (PM25 > 115 && PM25 <= 150) {
                bph = 150;
                bpl = 115;
                IAl = 150;
                IAh = 200;
            } else if (PM25 > 150 && PM25 <= 250) {
                bph = 250;
                bpl = 150;
                IAl = 200;
                IAh = 300;
            } else if (PM25 > 250 && PM25 <= 350) {
                bph = 350;
                bpl = 250;
                IAl = 300;
                IAh = 400;
            } else if (PM25 > 350 && PM25 <= 500) {
                bph = 500;
                bpl = 350;
                IAl = 400;
                IAh = 500;
            }
            PM25AQI = ((IAh - IAl) / (bph - bpl)) * (PM25 - bpl) + IAl;
        }
        if (PM10 > 0) {
            double bph = 0;
            double bpl = 0;
            double IAh = 0;
            double IAl = 0;
            if (0 < PM10 && PM10 <= 50) {
                bph = 35;
                IAh = 50;
            } else if (PM10 > 50 && PM10 <= 150) {
                bph = 75;
                bpl = 35;
                IAl = 50;
                IAh = 100;
            } else if (PM10 > 150 && PM10 <= 250) {
                bph = 250;
                bpl = 150;
                IAl = 100;
                IAh = 150;
            } else if (PM10 > 250 && PM10 <= 350) {
                bph = 350;
                bpl = 250;
                IAl = 150;
                IAh = 200;
            } else if (PM10 > 350 && PM10 <= 420) {
                bph = 420;
                bpl = 350;
                IAl = 200;
                IAh = 300;
            } else if (PM10 > 420 && PM10 <= 500) {
                bph = 500;
                bpl = 420;
                IAl = 300;
                IAh = 400;
            } else if (PM10 > 500 && PM10 <= 600) {
                bph = 600;
                bpl = 500;
                IAl = 400;
                IAh = 500;
            }
            PM10AQI = ((IAh - IAl) / (bph - bpl)) * (PM10 - bpl) + IAl;
        }
        double[] a = new double[6];
        a[0] = so2AQI;
        a[1] = NOAQI;
        a[2] = PM10AQI;
        a[3] = CoAQI;
        a[4] = PM25AQI;
        a[5] = O3AQI;
        double min = a[0];
        double max = a[0];
        for (int i = 0; i < 6; i++) {
            if (a[i] < min) {
                max = a[i];
            }
        }
        for (int i = 0; i < 6; i++) {
            if (a[i] > max) {
                max = a[i];
            }
        }
        int value = (int) (max + 0.5);
        return value;
    }

    public static String getAirClass(int num) {
        String str = "优";
        if (num >= 0 && num <= 50) {
            str = "优";
        } else if (num >= 51 && num <= 100) {
            str = "良";
        } else if (num >= 101 && num <= 150) {
            str = "轻度";
        } else if (num >= 151 && num <= 200) {
            str = "中度";
        } else if (num >= 201 && num <= 300) {
            str = "重度";
        } else if (num > 300) {
            str = "严重";
        }
        return str;
    }
    public static String compressString(String uncompressed) throws IOException {
        return uncompressed;
        //ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        //try (GZIPOutputStream gzipOutputStream = new GZIPOutputStream(outputStream)) {
        //    gzipOutputStream.write(uncompressed.getBytes(StandardCharsets.UTF_8));
        //}
        //return Base64.getEncoder().encodeToString(outputStream.toByteArray());
    }

    public static String decompressString(String compressed) throws IOException {
        byte[] compressedBytes = Base64.getDecoder().decode(compressed);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(compressedBytes);
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try (GZIPInputStream gzipInputStream = new GZIPInputStream(inputStream)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = gzipInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
        }
        return new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
    }
}

